package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import DataStorageClasses.Employee;
import DataStorageClasses.TimeSheet;
import Database.DatabaseConnector;

@WebServlet("/GetTimeSheetServlet")
public class GetTimeSheetServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	{
		DatabaseConnector db = new DatabaseConnector();
		
		long empID = -1;
		long jobID = -1;
		String high = request.getParameter("high");
		String low = request.getParameter("low");
		Date highDate=null, lowDate=null;
		String business;
		
		business = (String) request.getSession().getAttribute("username");
		if (business == null) {
			JSONObject result = new JSONObject();
			try {
				result.put("failed", true);
				result.put("message", "You Are Not Signed In");
				response.getWriter().print(result);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				response.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
				return;
			} 
			
			response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
			return;
		} 
		
		try {
			empID = Long.parseLong(request.getParameter("empID"));
		} catch(NullPointerException e) {
		} catch(NumberFormatException e) {
		}
		
		try {
			jobID = Long.parseLong(request.getParameter("jobID"));
		} catch(NullPointerException e) {
		} catch(NumberFormatException e) {
		}
		
		Calendar calendar = Calendar.getInstance();
		if (high != null) {
			String[] date1 = low.split("-");
			String[] date2 = high.split("-");
			try {
				calendar.set(Integer.parseInt(date1[0]), Integer.parseInt(date1[1])-1, Integer.parseInt(date1[2]));
				lowDate = new Date(calendar.getTimeInMillis());
			} catch (NumberFormatException e) {
			} catch (ArrayIndexOutOfBoundsException e) {
			}
			try {
				calendar.set(Integer.parseInt(date2[0]), Integer.parseInt(date2[1])-1, Integer.parseInt(date2[2]));
				highDate = new Date(calendar.getTimeInMillis());
			} catch (NumberFormatException e) {
			} catch (ArrayIndexOutOfBoundsException e) {
			}
		}
		
		ArrayList<TimeSheet> sheets = db.getTimeSheets(jobID, empID, lowDate, highDate, business);
		
		try {
			JSONObject result = new JSONObject();
			JSONArray jArray = new JSONArray();
		
			for (TimeSheet sheet: sheets) {
				Employee employee = db.getEmployee(sheet.getEmployeeID(), business);
				JSONObject json = sheet.getJSONObject();
				json.put("name", employee.getFirstName() + " " + employee.getLastName());
				jArray.put(json);
			}
			
			request.getSession().setAttribute("jobListRequest", request.getRequestURI() + "?" + request.getQueryString());
			result.put("timeSheets", jArray);
			result.put("failed", false);
			response.getWriter().print(result);
			response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
		} catch (JSONException e) {
			e.printStackTrace();
			response.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
		} catch (IOException e) {
			e.printStackTrace();
			response.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
		}
	}
}
